package cn.hairui.blog.util;

import java.util.*;

/**
 * @author lihairui
 * @version V1.0
 * @date: 2019/6/20 23:29
 */
public class CommUtil {

    /**
     * 将对象装换为map
     */
    public static Map<?, ?> objectToMap(Object obj) {
        if (obj == null) {
            return null;
        }
        return new org.apache.commons.beanutils.BeanMap(obj);
    }

    /**
     * 创建标签随机颜色
     *
     * @return
     */
    public static String createRandomColor() {

        String r, g, b;
        Random random = new Random();
        r = Integer.toHexString(random.nextInt(256)).toUpperCase();
        g = Integer.toHexString(random.nextInt(256)).toUpperCase();
        b = Integer.toHexString(random.nextInt(256)).toUpperCase();

        r = r.length() == 1 ? "0" + r : r;
        g = g.length() == 1 ? "0" + g : g;
        b = b.length() == 1 ? "0" + b : b;

        return "#" + r + g + b;
    }

    /**
     * 将tag标签字符串转换成list
     *
     * @param tagsStr
     * @return
     */
    public static List tagStrToList(String tagsStr) {
        if (tagsStr == null)
            return null;
        List list = new ArrayList();
        String[] tags = tagsStr.split("/");
        for (String tag : tags) {
            String tagStr = tag.replaceAll("#", "");
            if (!tagStr.trim().equals("")) {
                list.add(tagStr);
            }
        }
        return list;
    }

    public static String getUUIDUpperCase() {
        return UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
    }
}
